/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package objects;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Nam
 */
public class Map implements Serializable
{
    private ArrayList<ScanningLocation> locationList;
    private String mapName;
    public Map(String m_mapName)
    {
        locationList = new ArrayList<ScanningLocation>();
        mapName = m_mapName;
    }
    public Map()
    {
        
    }
    
    public String Localize(ScanningLocation lc)
    {
        double smallestDistance = 10000;
        String yourPlace = "not in range";
        Iterator<ScanningLocation> itr = this.locationList.iterator();
        while (itr.hasNext())
        {
            ScanningLocation m_location = itr.next();
            if ((m_location.getM_APList().size() ==lc.getM_APList().size()) && smallestDistance >= m_location.computeDistance(lc))
            {
                smallestDistance =  m_location.computeDistance(lc);
                yourPlace = m_location.getLocationName();
                System.out.println(m_location.getLocationName()+ ": " + smallestDistance);
            }
            else if(m_location.getM_APList().size() <= lc.getM_APList().size()+2 && m_location.getM_APList().size() > lc.getM_APList().size() && smallestDistance >= m_location.computeDistanceSubset(lc))
            {
                smallestDistance =  m_location.computeDistanceSubset(lc);
                yourPlace = m_location.getLocationName();
                System.out.println(m_location.getLocationName()+ ": " + smallestDistance);
            }
            /*else if(lc.getM_APList().size() <= m_location.getM_APList().size()+2 && lc.getM_APList().size() > m_location.getM_APList().size() && smallestDistance >= lc.computeDistanceSubset(m_location))
            {
                smallestDistance =  lc.computeDistanceSubset(m_location);
                yourPlace = m_location.getLocationName();
                System.out.println(m_location.getLocationName()+ ": " + smallestDistance);
            }*/
        }
        if (smallestDistance < 10000)
            return yourPlace;
        return "not in range";
    }
    //add new location into map file
    public void addLocation(ScanningLocation m_location)
    {
        this.locationList.add(m_location);
    }

    public ScanningLocation getLocationAt(int index)
    {
        return this.locationList.get(index);
    }

    //create a map file from list of ScanningLocation
    public int createMapFile()
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(getMapName());
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            for(int i =0;i<locationList.size();i++)
            {
                ScanningLocation item = locationList.get(i);
                oos.writeObject(item);
            }
            //close
            fos.close();
            oos.close();
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }

    public int createBlankMapFile(File m_file)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(m_file);
            //close
            fos.close();
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }

    public int createMapFile(File m_file)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(m_file, false);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            for(int i =0;i<locationList.size();i++)
            {
                ScanningLocation item = locationList.get(i);
                oos.writeObject(item);
            }
            //close
            fos.close();
            oos.close();
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }

    //read a map file
    public Map readMapFile(File inputFile)
    {
        //create new instance Map file
        Map newMap = new Map(inputFile.getName());
        ScanningLocation p = null;
        //read from input file
        try
        {
            FileInputStream fis = new FileInputStream(inputFile);
            ObjectInputStream ois = new ObjectInputStream(fis);

            while(ois.available()>-1)
            {
                p = (ScanningLocation)ois.readObject();
                if(p == null)
                    break;
                newMap.addLocation(p); //add founded location into map file
            }
            fis.close();
            ois.close();
        }
        catch(Exception e)
        {
            System.out.println("Finish Reading");
        }
        
        return newMap;
    }

    public void printResult()
    {
        if(this.locationList.size()>0)
        {
            for(int i = 0; i < locationList.size();i++)
            {
                ScanningLocation _location = locationList.get(i);
                _location.printResult();
            }
        }
    }

    public String convertMapToString()
    {
        String content = "";
        if(this.locationList.size()>0)
        {
            for(int i=0;i<locationList.size();i++)
            {
                ScanningLocation _location = locationList.get(i);
                content+= _location.getResult();
            }
        }
        return content;
    }
    /**
     * @return the mapName
     */
    public String getMapName() {
        return mapName;
    }

    /**
     * @param mapName the mapName to set
     */
    public void setMapName(String mapName) {
        this.mapName = mapName;
    }
    


    /**
     * @return the locationList
     */
    public ArrayList<ScanningLocation> getLocationList() {
        return locationList;
    }

    /**
     * @param locationList the locationList to set
     */
    public void setLocationList(ArrayList<ScanningLocation> locationList) {
        this.locationList = locationList;
    }
}
